//
//  FangwenDetailViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/21.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "FangwenDetailViewController.h"

#import "FangwenDetailTableViewCell.h"
#import "TubiaoViewController_1.h"
#import "TubiaoViewController_2.h"

@interface FangwenDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

//头视图
@property (nonatomic, strong) UIView * view_head;

@property (nonatomic, strong) UILabel * koufei_1;
@property (nonatomic, strong) UILabel * koufei_2;
@property (nonatomic, strong) UILabel * koufei_3;

@property (nonatomic, strong) UILabel * scale_1;
@property (nonatomic, strong) UILabel * scale_2;
@property (nonatomic, strong) UILabel * scale_3;

@property (nonatomic, strong) UIButton * btn_1;
@property (nonatomic, strong) UIButton * btn_2;
@property (nonatomic, strong) UIButton * btn_3;

@property (nonatomic, strong) NSString * str_btnType;

//
@property (nonatomic, assign) NSInteger number;


@end

@implementation FangwenDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.str_btnType = @"0";
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"%@",self.str_webName];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    _btnRight.hidden = YES;
    
    [self addLeftButton:@"fanhui@2x.png"];
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//隐藏tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self p_headView];
    self.tableView.tableHeaderView = self.view_head;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[FangwenDetailTableViewCell class] forCellReuseIdentifier:@"cell_fangwenDetail"];
    
    
    //刷新
    __unsafe_unretained UITableView *tableView = self.tableView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data];
        // 结束刷新
        [tableView.mj_header endRefreshing];
    }];
    
    // 马上进入刷新状态
    [tableView.mj_header beginRefreshing];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
    // 上拉加载
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [weakSelf p_data1];
        // 结束刷新
        [tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - 数据
- (void )p_data
{
    self.str_btnType = @"0";
    
    self.number = 15;

    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

- (void )p_data1
{
    self.number = self.number + 10;

    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.number;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FangwenDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_fangwenDetail" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //假数据
    cell.source.text = @"百度PC端";
    cell.date.text = [NSString stringWithFormat:@"2016-3-%d 12:%d",indexPath.row + 1,indexPath.row + 10];
    cell.str_IP.text = [NSString stringWithFormat:@"IP地址:255.255.255.%d[山东省临沂市]",indexPath.row + 100];
    
    
    return cell;
}

- (UIView * )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    

    
    UILabel * text_2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 85, 20)];
    text_2.text = @"访问数据列表";
    //    text_2.backgroundColor = [UIColor orangeColor];
    text_2.font = [UIFont systemFontOfSize:14];
    text_2.textColor = [UIColor blackColor];
    [view addSubview:text_2];
    
    
    UIView * view_bg2 = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 110 - 5, 5, 110, 30)];
//    view_bg2.backgroundColor = [UIColor orangeColor];
    [view addSubview:view_bg2];
    
    CGFloat length_btnX = 110 / 3;
    
    //昨天
    self.btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_1.frame = CGRectMake(0, 0, length_btnX, 30);
    [self.btn_1 setTitle:@"昨日" forState:(UIControlStateNormal)];
    self.btn_1.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.btn_1 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [self.btn_1 addTarget:self action:@selector(btn_1Action:) forControlEvents:(UIControlEventTouchUpInside)];
    [view_bg2 addSubview:self.btn_1];
    
    //    self.btn_1.layer.cornerRadius = 10;
    //    self.btn_1.layer.borderColor = navi_bar_bg_color.CGColor;
    //    self.btn_1.layer.borderWidth = 1;
    
    
    //昨7天
    self.btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_2.frame = CGRectMake(length_btnX, 0, length_btnX, 30);
    [self.btn_2 setTitle:@"7日" forState:(UIControlStateNormal)];
    self.btn_2.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.btn_2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [self.btn_2 addTarget:self action:@selector(btn_2Action:) forControlEvents:(UIControlEventTouchUpInside)];
    [view_bg2 addSubview:self.btn_2];
    
    //    self.btn_2.layer.cornerRadius = 10;
    //    self.btn_2.layer.borderColor = [UIColor blackColor].CGColor;
    //    self.btn_2.layer.borderWidth = 1;
    
    //昨30天
    self.btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_3.frame = CGRectMake(length_btnX * 2, 0, length_btnX, 30);
    [self.btn_3 setTitle:@"30日" forState:(UIControlStateNormal)];
    self.btn_3.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.btn_3 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    [self.btn_3 addTarget:self action:@selector(btn_3Action:) forControlEvents:(UIControlEventTouchUpInside)];
    [view_bg2 addSubview:self.btn_3];
    
    //    self.btn_3.layer.cornerRadius = 10;
    //    self.btn_3.layer.borderColor = [UIColor blackColor].CGColor;
    //    self.btn_3.layer.borderWidth = 1;
    
    switch ([self.str_btnType integerValue])
    {
        case 0:
        {
            [self.btn_1 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
        }
            break;
        case 1:
        {
            [self.btn_2 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
        }
            break;
        case 2:
        {
            [self.btn_3 setTitleColor:navi_bar_bg_color forState:(UIControlStateNormal)];
        }
            break;
        default:
            break;
    }

    return view;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0f;
}



#pragma mark - 头视图
- (void )p_headView
{
    CGFloat length_x = (SCREEN_WIDTH - 10) / 4;
    
    self.view_head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 280)];
    self.view_head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    UILabel * text = [[UILabel alloc] initWithFrame:CGRectMake(15 , 10, SCREEN_WIDTH - 5 - length_x / 2 - 27.5 - 15, 20)];
    text.text = @"访问统计";
    text.font = [UIFont systemFontOfSize:14];
    text.textColor = [UIColor blackColor];
    [self.view_head addSubview:text];
    
    
    //下面的数据展示
    UIImageView * view_bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 100)];
    //            view_bg.image = [UIImage imageNamed:@"whith_bg.png"];
    view_bg.backgroundColor = [UIColor whiteColor];
    [self.view_head addSubview:view_bg];
    
    //1
    UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 50 - 27.5, 55, 55)];
    image.image = [UIImage imageNamed:@"tongji2@2x.png"];
    [view_bg addSubview:image];
    
    //2
    self.koufei_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x - 5, 30, length_x + 8, 25)];
    self.koufei_1.text = @"28.22";
    self.koufei_1.font = [UIFont systemFontOfSize:15];
    //    self.koufei_1.backgroundColor = [UIColor orangeColor];
    self.koufei_1.textAlignment = NSTextAlignmentCenter;
    [view_bg addSubview:self.koufei_1];
    
    UILabel * label_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, CGRectGetMaxY(self.koufei_1.frame) + 5, length_x, 20)];
    label_1.text = @"昨日点击";
    label_1.font = [UIFont systemFontOfSize:14];
    label_1.textAlignment = NSTextAlignmentCenter;
    label_1.textColor = [UIColor grayColor];
    [view_bg addSubview:label_1];
    
    
    //3
    self.koufei_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, 30, length_x, 25)];
    self.koufei_2.text = @"892";
    self.koufei_2.font = [UIFont systemFontOfSize:15];
    //    self.koufei_2.backgroundColor = [UIColor orangeColor];
    self.koufei_2.textAlignment = NSTextAlignmentCenter;
    [view_bg addSubview:self.koufei_2];
    
    UILabel * label_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, CGRectGetMaxY(self.koufei_1.frame) + 5, length_x, 20)];
    label_2.text = @"七天内";
    label_2.font = [UIFont systemFontOfSize:14];
    label_2.textAlignment = NSTextAlignmentCenter;
    label_2.textColor = [UIColor grayColor];
    [view_bg addSubview:label_2];
    
    
    //4
    self.koufei_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, 30, length_x, 25)];
    self.koufei_3.text = @"81";
    self.koufei_3.font = [UIFont systemFontOfSize:15];
    //    self.koufei_2.backgroundColor = [UIColor orangeColor];
    self.koufei_3.textAlignment = NSTextAlignmentCenter;
    [view_bg addSubview:self.koufei_3];
    
    UILabel * label_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, CGRectGetMaxY(self.koufei_1.frame) + 5, length_x, 20)];
    label_3.text = @"30天";
    label_3.font = [UIFont systemFontOfSize:14];
    label_3.textAlignment = NSTextAlignmentCenter;
    label_3.textColor = [UIColor grayColor];
    [view_bg addSubview:label_3];
    
    
    UILabel * text_1 = [[UILabel alloc] initWithFrame:CGRectMake(15 , CGRectGetMaxY(view_bg.frame) + 10, SCREEN_WIDTH - 5 - length_x / 2 - 27.5 - 15, 20)];
    text_1.text = @"数据分析";
    text_1.font = [UIFont systemFontOfSize:14];
    text_1.textColor = [UIColor blackColor];
    [self.view_head addSubview:text_1];
    
    
    //下面的数据展示
    UIImageView * view_bg1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(text_1.frame) + 10, SCREEN_WIDTH, 100)];
    //    view_bg1.image = [UIImage imageNamed:@"whith_bg.png"];
    view_bg1.backgroundColor = [UIColor whiteColor];
    [self.view_head addSubview:view_bg1];
    
    
    //1
    UIImageView * image1 = [[UIImageView alloc] initWithFrame:CGRectMake(5 + length_x / 2 - 27.5, 50 - 27.5, 55, 55)];
    image1.image = [UIImage imageNamed:@"fenxi2@2x.png"];
    [view_bg1 addSubview:image1];
    
    //2
    self.scale_1 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x - 5, 30, length_x + 8, 25)];
    self.scale_1.text = @"18.22";
    self.scale_1.font = [UIFont systemFontOfSize:15];
    //    self.scale_1.backgroundColor = [UIColor orangeColor];
    self.scale_1.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:self.scale_1];
    
    UILabel * label_4 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x, CGRectGetMaxY(self.scale_1.frame) + 5, length_x, 20)];
    label_4.text = @"日均访问";
    label_4.font = [UIFont systemFontOfSize:14];
    label_4.textAlignment = NSTextAlignmentCenter;
    label_4.textColor = [UIColor grayColor];
    [view_bg1 addSubview:label_4];
    
    
    //3
    self.scale_2 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, 30, length_x, 25)];
    self.scale_2.text = @"65%";
    self.scale_2.font = [UIFont systemFontOfSize:15];
    //    self.scale_2.backgroundColor = [UIColor orangeColor];
    self.scale_2.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:self.scale_2];
    
    UILabel * label_5 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 2, CGRectGetMaxY(self.scale_1.frame) + 5, length_x, 20)];
    label_5.text = @"电脑";
    label_5.font = [UIFont systemFontOfSize:14];
    label_5.textAlignment = NSTextAlignmentCenter;
    label_5.textColor = [UIColor grayColor];
    [view_bg1 addSubview:label_5];
    
    
    //4
    self.scale_3 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, 30, length_x, 25)];
    self.scale_3.text = @"23%";
    self.scale_3.font = [UIFont systemFontOfSize:15];
    //    self.scale_3.backgroundColor = [UIColor orangeColor];
    self.scale_3.textAlignment = NSTextAlignmentCenter;
    [view_bg1 addSubview:self.scale_3];
    
    UILabel * label_6 = [[UILabel alloc] initWithFrame:CGRectMake(5 + length_x * 3, CGRectGetMaxY(self.scale_1.frame) + 5, length_x, 20)];
    label_6.text = @"手机";
    label_6.font = [UIFont systemFontOfSize:14];
    label_6.textAlignment = NSTextAlignmentCenter;
    label_6.textColor = [UIColor grayColor];
    [view_bg1 addSubview:label_6];
    

    //手势
    view_bg.userInteractionEnabled = YES;
    view_bg1.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [view_bg addGestureRecognizer:tapGesture];
    
    
    UITapGestureRecognizer * tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture1:)];
    tapGesture1.numberOfTapsRequired = 1; //点击次数
    tapGesture1.numberOfTouchesRequired = 1; //点击手指数
    [view_bg1 addGestureRecognizer:tapGesture1];
}

#pragma mark - 图表视图
- (void)tapGesture:(id)sender
{
    TubiaoViewController_1 * tubiaoViewController_1 = [[TubiaoViewController_1 alloc] init];
    [self showViewController:tubiaoViewController_1 sender:nil];
}

- (void)tapGesture1:(id)sender
{
    TubiaoViewController_2 * tubiaoViewController_2 = [[TubiaoViewController_2 alloc] init];
    [self showViewController:tubiaoViewController_2 sender:nil];
}

#pragma mark - 点击昨天, 7 , 30

- (void )btn_1Action:(UIButton *)sender
{
    self.str_btnType = @"0";
    
    self.number = 15;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

- (void )btn_2Action:(UIButton *)sender
{
    self.str_btnType = @"1";

    self.number = 65;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
    
}


- (void )btn_3Action:(UIButton *)sender
{
    self.str_btnType = @"2";

    self.number = 430;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}




@end
